package tjo;

import static org.junit.Assert.assertEquals;
import robocode.control.testing.RobotTestBed;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;

/**
 * This test verifies that Meteor always beats Fire.
 * 
 * @author Jordan Takayama
 * @since October 6, 2011
 */
public class TestMeteorVersusFire extends RobotTestBed {
  /**
   * The total matches to be played by Meteor against Corners.
   */
  private int matches = 10;

  /**
   * The minimum matches Meteor must win against Corners.
   */
  private int minimumWins = (int) Math.ceil(0.6 * matches);

  /**
   * Specifies that Fire and Meteor are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Fire,tjo.Meteor";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return matches;
  }

  /**
   * The actual test, which asserts that Meteor has won at least 60% of matches against Fire.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    // Return the results in order of getRobotNames.
    BattleResults[] battleResults = event.getIndexedResults();
    // Sanity check that results[1] is Meteor (not strictly necessary, but illustrative).
    BattleResults meteorResults = battleResults[1];
    String robotName = meteorResults.getTeamLeaderName();
    assertEquals("Check that results[1] is Meteor", robotName, "tjo.Meteor*");

    // Check to make sure that Meteor won at least six of the ten rounds.
    System.out.println("Check that Meteor won at least 60% of matches:");
    assert (minimumWins <= meteorResults.getFirsts());
  }

  /**
   * Returns a comma or space separated list like: x1,y1,heading1, x2,y2,heading2, which are the
   * coordinates and heading of robot #1 and #2. So "0,0,180, 50,80,270" means that robot #1 has
   * position (0,0) and heading 180, and robot #2 has position (50,80) and heading 270.
   * 
   * Override this method to explicitly specify the initial positions for your test cases.
   * 
   * Defaults to null, which means that the initial positions are determined randomly. Since battles
   * are deterministic by default, the initial positions are randomly chosen but will always be the
   * same each time you run the test case.
   * 
   * @return The list of initial positions.
   */
  @Override
  public String getInitialPositions() {
    return null;
  }

  /**
   * Returns true if the battle should be deterministic and thus robots will always start in the
   * same position each time.
   * 
   * Override to return false to support random initialization.
   * 
   * @return True if the battle will be deterministic.
   */
  @Override
  public boolean isDeterministic() {
    return false;
  }

  /**
   * Specifies how many errors you expect this battle to generate. Defaults to 0. Override this
   * method to change the number of expected errors.
   * 
   * @return The expected number of errors.
   */
  @Override
  protected int getExpectedErrors() {
    return 0;
  }
}
